package com.facebank.usersupport.service.impl;

import com.facebank.usersupport.dto.ActivityDto;
import com.facebank.usersupport.dto.AssocitatorDto;
import com.facebank.usersupport.dto.FinanceDto;
import com.facebank.usersupport.mapper.usersupport.usersupport.ActivityMapper;
import com.facebank.usersupport.mapper.usersupport.usersupport.AssMonthActiveMapper;
import com.facebank.usersupport.mapper.usersupport.usersupport.AssMonthMapper;
import com.facebank.usersupport.mapper.usersupport.usersupport.DgutOrderMapper;
import com.facebank.usersupport.service.ICountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: yaozun
 * @Date: 2018/3/23 0023 14:25
 * @Description:
 */
@Service
public class CountServiceImpl implements ICountService {
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private AssMonthMapper assMonthMapper;
    @Autowired
    private AssMonthActiveMapper assMonthActiveMapper;
    @Autowired
    private DgutOrderMapper dgutOrderMapper;

    @Override
    public List<ActivityDto> activityCount(Long startTime, Long endTime) {
        List<ActivityDto> activityDtos = new ArrayList<>();
        //省内活动次数
        ActivityDto activityDto1 = new ActivityDto();
        activityDto1.setName("省内活动");
        List<Integer> list1 = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            list1.add(activityMapper.activityCount(startTime, endTime, i, (short) 1));
        }
        activityDto1.setData(list1);
        //省外活动次数
        ActivityDto activityDto2 = new ActivityDto();
        activityDto2.setName("省外活动");
        List<Integer> list2 = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            list2.add(activityMapper.activityCount(startTime, endTime, i, (short) 2));
        }
        activityDto2.setData(list2);
        //国外活动
        ActivityDto activityDto3 = new ActivityDto();
        activityDto3.setName("国外活动");
        List<Integer> list3 = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            list3.add(activityMapper.activityCount(startTime, endTime, i, (short) 3));
        }
        activityDto3.setData(list3);

        activityDtos.add(activityDto1);
        activityDtos.add(activityDto2);
        activityDtos.add(activityDto3);
        return activityDtos;

    }

    @Override
    public List<AssocitatorDto> associatorCount(Long startTime, Long endTime) {
        List<AssocitatorDto> associtatorDtos = new ArrayList<>();
        //新增客户
        AssocitatorDto associtatorDto1 = new AssocitatorDto();
        associtatorDto1.setName("新增客户");
        List<Integer> list1 = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            list1.add(assMonthMapper.assMonthCount(startTime, endTime, i));
        }
        associtatorDto1.setData(list1);
        //活跃客户
        AssocitatorDto associtatorDto2 = new AssocitatorDto();
        associtatorDto2.setName("活跃客户");
        List<Integer> list2 = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            list2.add(assMonthActiveMapper.assMonthActiveCount(startTime, endTime, i));
        }
        associtatorDto2.setData(list2);

        associtatorDtos.add(associtatorDto1);
        associtatorDtos.add(associtatorDto2);
        return associtatorDtos;
    }

    @Override
    public List<FinanceDto> financeCount(Long startTime, Long endTime) {
        BigDecimal totalMoney = dgutOrderMapper.amountSum(startTime,endTime);
        List<FinanceDto> financeDtos = new ArrayList<>();
        FinanceDto financeDto1 = new FinanceDto();
        FinanceDto financeDto2 = new FinanceDto();
        FinanceDto financeDto3 = new FinanceDto();
        financeDto1.setName("收益");
        financeDto2.setName("活动成本");
        financeDto3.setName("代理金");
        financeDto1.setValue(totalMoney.multiply(new BigDecimal(0.42)).setScale(2,BigDecimal.ROUND_HALF_UP));
        financeDto2.setValue(totalMoney.multiply(new BigDecimal(0.48)).setScale(2,BigDecimal.ROUND_HALF_UP));
        financeDto3.setValue(totalMoney.multiply(new BigDecimal(0.1)).setScale(2,BigDecimal.ROUND_HALF_UP));
        financeDtos.add(financeDto1);
        financeDtos.add(financeDto2);
        financeDtos.add(financeDto3);

        return financeDtos;
    }
}
